//
//  Left_ViewController.m
//  International_Hospital
//
//  Created by LiYiLin on 16/7/4.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "Left_ViewController.h"
//名医通申请
#import "DoctorApply_ViewController.h"
//病历管理
#import "CaseManage_ViewController.h"
//关注医生

//设置与帮助
#import "Setting_ViewController.h"
//关于产品
#import "AboutProduct_ViewController.h"
//分享给好友
#import "Shared_ViewController.h"
//登录
#import "Login_ViewController.h"
//注册
#import "Register_ViewController.h"

//自定义的button
#import "IH_Button.h"

//医生信息
#import "DoctorInfo_ViewController.h"

//关注医生列表
#import "Doctor_listTableViewController.h"

//病人信息
#import "DiseaseInfo_viewController.h"





@interface Left_ViewController ()<UITableViewDelegate,UITableViewDataSource,LoginViewControllerDelegate>

//无登录界面
@property (nonatomic, strong) UIView * noLoginView;
//有登录界面
@property (nonatomic, strong) UIView * loginView;
//tableView
@property (nonatomic, strong) UITableView * tableView;
//用户名Lable
@property (nonatomic, weak) UILabel * username_Label;

//储存第一组cell标签内容
@property (nonatomic, strong) NSArray * arrayData1;
//储存第二组cell标签内容
@property (nonatomic, strong) NSArray * arrayData2;


//设置属性判断加载哪个界面(ljq)
@property (nonatomic,assign)BOOL loginOrOff;

@property (nonatomic, weak) UIImageView * tableViewBack_IMG;
//设置用户头像
@property (nonatomic,strong)UIImageView *userImageView;


@end

//cell的重用标识符
static NSString * identifier = @"cell";

@implementation Left_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configure_UI];

    //读取用户偏好存储的信息
    self.loginOrOff =[[NSUserDefaults standardUserDefaults] boolForKey:@"loadOrOff"];
    //加载界面
    [self setup_UI];
}

#pragma mark - 右侧的
- (void)configure_UI {
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setHidden:YES];
    
    self.arrayData1 = @[@"名医通申请",@"病例管理",@"关注医生"];
    self.arrayData2 = @[@"设置与帮助",@"关于产品",@"分享给好友"];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
}

#pragma mark - 配置页面
- (void)setup_UI {
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = kThemeColor;
    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"123"]];
    self.tableView.backgroundView = imageView;
    
    imageView.bounds = self.tableView.bounds;
    
    if (self.loginOrOff) {
    
        [self.view addSubview:self.loginView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.loginView.mas_bottom);
            make.left.bottom.mas_equalTo(0);
            //      SCREEN_LEFT_WIDTH ([UIScreen mainScreen].bounds.size.width*0.8)
            make.width.mas_equalTo(SCREEN_LEFT_WIDTH);
        }];
        
    } else {
        [self.view addSubview:self.noLoginView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.noLoginView.mas_bottom);
            make.left.bottom.mas_equalTo(0);
            //      SCREEN_LEFT_WIDTH ([UIScreen mainScreen].bounds.size.width*0.8)
            make.width.mas_equalTo(SCREEN_LEFT_WIDTH);
        }];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //需要进行判断是一组还是两组 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if (!self.loginOrOff) {
        return 0;
    }
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    //判断登录是否成功,成功直接返回,因为只有一组数据
    if (!self.loginOrOff) {
        cell.textLabel.text = self.arrayData2[indexPath.row];
        return cell;
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = self.arrayData1[indexPath.row];
    } else {
        cell.textLabel.text = self.arrayData2[indexPath.row];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    }
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    UIViewController * new_ViewController = nil;
    
    if (section == 0) {
        switch (row) {
            case 0:
                new_ViewController = (DoctorApply_ViewController *)[[DoctorApply_ViewController alloc]init];
                break;
            case 1:
                new_ViewController = (CaseManage_ViewController *) [[CaseManage_ViewController alloc]init];
                break;
            case 2:
                new_ViewController = (Doctor_listTableViewController *)[[Doctor_listTableViewController alloc]init];
                
                break;
            default:
                break;
        }
        new_ViewController.title = self.arrayData1[row];
        
    } else {
        switch (row) {
            case 0:
                new_ViewController = [(Setting_ViewController *)[Setting_ViewController alloc] init];
                break;
            case 1:
                new_ViewController = [(AboutProduct_ViewController *)[AboutProduct_ViewController alloc] init];
                break;
            case 2:
                new_ViewController = [(Shared_ViewController *)[Shared_ViewController alloc] init];
                break;
            default:
                break;
        }
        new_ViewController.title = self.arrayData2[row];
    }
#pragma 给病例管理
//    if ([new_ViewController isKindOfClass:[CaseManage_ViewController class]]) {
//        
//        CaseManage_ViewController *caseVc = (CaseManage_ViewController *)new_ViewController;
//        caseVc.dataDic = self.dataDic;
//        
//    }
    [self jumpToOtherViewController:new_ViewController];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - tableView懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

#pragma mark - 没有登录时的样式
- (UIView *)noLoginView {
    
    if (_noLoginView == nil) {
        _noLoginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
        _noLoginView.backgroundColor = kThemeColor;
        
        //头像
        UIImageView * icon = [[UIImageView alloc] init];
        icon.image = [UIImage imageNamed:@"name"];
        
        
        [_noLoginView addSubview:icon];
        icon.bounds = CGRectMake(0, 0, 45, 45);
//      SCREEN_LEFT_WIDTH ([UIScreen mainScreen].bounds.size.width*0.8)
        icon.center = CGPointMake(SCREEN_LEFT_WIDTH / 2, 50);
        
        //登录按钮
        UIButton * left_Button = [[UIButton alloc] init];
        [_noLoginView addSubview:left_Button];
        [left_Button setTag:101];
        [left_Button setTitle:@"登录" forState:UIControlStateNormal];
        [left_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [left_Button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [left_Button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(icon.mas_bottom).offset(10);
            make.right.mas_equalTo(icon.mas_centerX).offset(-10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
        //注册按钮
        UIButton * right_Button = [[UIButton alloc] init];
        [_noLoginView addSubview:right_Button];
        [right_Button setTag:102];
        [right_Button setTitle:@"注册" forState:UIControlStateNormal];
        [right_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [right_Button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [right_Button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(icon.mas_bottom).offset(10);
            make.left.mas_equalTo(icon.mas_centerX).offset(10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
    }
    return _noLoginView;
}

#pragma mark - 已经登录时的样式
- (UIView *)loginView {

    if (_loginView == nil) {
        _loginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
        _loginView.backgroundColor = kThemeColor;
        
        //用户头像
        self.userImageView = [[UIImageView alloc] init];
        
        NSFileManager *manger = [NSFileManager defaultManager];
        if ([manger fileExistsAtPath:PATH]) {
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:PATH]];
            self.userImageView.image = image;
        }else{
            self.userImageView.image = [UIImage imageNamed:@"Bill"];
        }
        
        
        
        
        
//        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//        NSString *imgKey = [defaults objectForKey:@"imgName"];
//        if (imgKey) {
//            icon.image = [UIImage imageNamed:imgKey];
//        }
        
        self.userImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didIconImageView:)];
        [self.userImageView addGestureRecognizer:tap];
        
        [_loginView addSubview: self.userImageView];
        self.userImageView.layer.cornerRadius = 35;
        self.userImageView.layer.masksToBounds = YES;
        
        [self.userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(70, 70));
            make.left.mas_equalTo(20);
            make.top.mas_equalTo(30);
        }];
        
        //用户昵称
        UILabel * username_Label = [[UILabel alloc] init];
        self.username_Label = username_Label;
        username_Label.text = @"比尔盖茨";
        
        
        
        NSString *name = self.dataDic[@"姓名"];
        if (name) {
            self.username_Label.text = name;
        }
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *nameKey = [defaults objectForKey:@"姓名"];
        if (nameKey) {
            self.username_Label.text = nameKey;
        }
        
        username_Label.font = FONT(16);
        [_loginView addSubview:username_Label];
        
        [username_Label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userImageView).offset(5);
            make.left.mas_equalTo(self.userImageView.mas_right).offset(15);
        }];
        
        //个人信息
        IH_Button * description_button = [IH_Button buttonWithTitle:@"查看个人信息"];
        [_loginView addSubview:description_button];
        
        [description_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.userImageView).offset(-5);
            make.left.mas_equalTo(self.userImageView.mas_right).offset(15);
        }];
        
        // 添加按钮点击事件
        [description_button addTarget:self action:@selector(didLookInfo:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginView;
}

#pragma mark - 点击按钮触发
- (void)clickButton:(UIButton *)button {
    if (button.tag == 101) {
        Login_ViewController * login_VC = [[Login_ViewController alloc] init];
        //mark 设置左侧控制器成为登录控制器代理
        login_VC.delegate =self;
        [self jumpToOtherViewController:login_VC];
    } else {
        Register_ViewController * register_VC = [[Register_ViewController alloc] init];
        [self jumpToOtherViewController:register_VC];
    }
}

#pragma 实现代理方法
-(void)loginViewController:(Login_ViewController *)loginViewController addLoginSuccess:(BOOL)success addInitDic:(NSDictionary *)dic{
    
    self.dataDic = dic;
    
    self.loginOrOff = success;
    
    

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    
    
    [defaults setBool:self.loginOrOff forKey:@"loadOrOff"];
    
    [defaults synchronize];
    [self setup_UI];
    [self.tableView reloadData];
}

#pragma 点击查看个人信息
-(void)didLookInfo:(IH_Button*)button{
    
    //跳转到个人信息控制器
    DiseaseInfo_viewController *disViewController = [[DiseaseInfo_viewController alloc]init];
    disViewController.title = [NSString stringWithFormat:@"%@的个人信息",self.username_Label.text];
    disViewController.disBlock = ^(NSString*name){
        
        self.username_Label.text = name;
        
    };

    disViewController.dicBlock = ^(NSDictionary *dataDic){

        self.dataDic = dataDic;
    };
    disViewController.picBlock = ^(UIImage *image){
        
        self.userImageView.image= image;
        
    };
    
    [self jumpToOtherViewController:disViewController];
    
    
    
}


-(void)didIconImageView:(UIGestureRecognizer *)ges{
    
    DiseaseInfo_viewController *desVc = [[DiseaseInfo_viewController alloc]init];
    
    desVc.title = [NSString stringWithFormat:@"%@的个人信息",self.username_Label.text];
    desVc.disBlock = ^(NSString*name){
        
        self.username_Label.text = name;
        
        [self viewDidLoad];
        
    };
    
    desVc.picBlock = ^(UIImage *image){
        
      self.userImageView.image= image;  
        
    };
    desVc.dicBlock = ^(NSDictionary *dataDic){
        self.dataDic = dataDic;

    };
    [self jumpToOtherViewController:desVc];
}

-(void)dealloc{
    
    NSLog(@"主页消失");
}



@end













